﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DataInput;

namespace DataInputUI
{
    class ControlFactory
    {        
        public static Control generateControl(Field field)
        {
            if (field.getOptions () == null)
            {
                TextBox textbox = new TextBox();                
                if (field.Value != null)
                    textbox.Text = field.Value.ToString();
                return textbox;
            }
            else
            {
                Dictionary<String, Object> values = field.getOptions();
                ListBox items = new ListBox();
                    
                foreach (KeyValuePair<String, Object> de in values)
                {
                    items.Items.Add(de.Key);
                    items.SetSelected(items.Items.Count - 1, false);
                    if (!(field is MultiListField) && field.Value != null && field.Value.ToString () == de.Value.ToString ())
                        items.SelectedIndex = items.Items.Count - 1;
                }
                if ( field is MultiListField )
                {
                    items.SelectionMode = SelectionMode.MultiExtended;
                    List<Object> selected = (List<Object>) field.Value;
                    for ( int j = 0; j < selected.Count; j++ )
                        for ( int i = 0; i < items.Items.Count; i++ )
                            if ( selected [ j ].ToString() == items.Items [ i ].ToString())
                                items.SetSelected(i, true);
                }
                return items;
            }
        }
    }
}
